package com.taotao.cart.service;

import com.github.abel533.entity.Example;
import com.taotao.cart.bean.Item;
import com.taotao.cart.bean.User;
import com.taotao.cart.mapper.CartMapper;
import com.taotao.cart.pojo.Cart;
import com.taotao.cart.threadlocal.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ItemService itemService;

    public void addItemToCart(Long itemId) {
        User user = UserThreadLocal.get();
        Cart record = new Cart();
        record.setItemId(itemId);
        record.setUserId(user.getId());
        Cart cart =  this.cartMapper.selectOne(record);
        if(null == cart){
            //不存在
            cart = new Cart();
            cart.setUserId(user.getId());
            cart.setCreated(new Date());
            cart.setCreated(cart.getCreated());

            Item item = this.itemService.queryById(itemId);

            cart.setItemId(itemId);
            cart.setItemTitle(item.getTitle());
            cart .setItemPrice(item.getPrice());
            cart.setItemImage(StringUtils.split(item.getImage(),',')[0]);
            cart.setNum(1);

            this.cartMapper.insert(cart);
        }else {
            cart.setNum(cart.getNum() + 1);
            cart.setUpdated(new Date());
            this.cartMapper.updateByPrimaryKey(cart);
        }
    }

    public List<Cart> queryCartlist(Long userId){
        Example example = new Example(Cart.class);
        //设置排序条件
        example.setOrderByClause("created DESC");

        //设置查询条件
        example.createCriteria().andEqualTo("userId",userId);

        return this.cartMapper.selectByExample(example);
    }

    public List<Cart> queryCartlist(){
        return this.queryCartlist(UserThreadLocal.get().getId());
    }

    public void updateNum(Long itemId, Integer num){
        //更新的数据
        Cart record = new Cart();
        record.setNum(num);
        record.setUpdated(new Date());

        //更新条件
        Example example = new Example(Cart.class);
        example.createCriteria().andEqualTo("itemId",itemId).andEqualTo("userId",UserThreadLocal.get().getId());
        this.cartMapper.updateByExampleSelective(record,example);
    }

    public void delete(Long itemId){
        Cart record = new Cart();
        record.setItemId(itemId);
        record.setUserId(UserThreadLocal.get().getId());
        this.cartMapper.delete(record);
    }

}
